package model;

import java.util.ArrayList;
import java.util.Iterator;

import model.trajectory.TargetModel;

import utils.Pair;
import utils.Point3D;
import utils.TimeIntervalle;

public class DoorModel extends BaseLinkModel
{
	public Orientation Orientation;
	public ArrayList<Pair<TimeIntervalle, ArrayList<TargetModel>>> targetsByIntervalle;
	
	public DoorModel(int ID, RoomModel firstRoom, RoomModel secondRoom, Point3D anchoragePoint, Orientation orientation)
	{
		super(ID, firstRoom, secondRoom, anchoragePoint);
		this.Orientation = orientation;
		this.targetsByIntervalle = new ArrayList<Pair<TimeIntervalle, ArrayList<TargetModel>>>();
	}

	public void addTarget(TimeIntervalle intervalle, TargetModel target) 
	{
		for(Iterator<Pair<TimeIntervalle, ArrayList<TargetModel>>> it = this.targetsByIntervalle.iterator(); it.hasNext();)
		{
			Pair<TimeIntervalle, ArrayList<TargetModel>> pair = it.next();
			if(pair.key.equals(intervalle))
			{
				int index = 0;
				for(Iterator<TargetModel> itTarget = pair.value.iterator(); itTarget.hasNext();)
				{
					TargetModel currentTarget = itTarget.next();
					if(currentTarget.getID() <= target.getID())
					{
						index++;
					}
				}
				pair.value.add(index, target);
			}
		}		
	}
	
	public ArrayList<TargetModel> targetsByTime(long time) 
	{
		ArrayList<TargetModel> result = new ArrayList<TargetModel>(); 
		for(Iterator<Pair<TimeIntervalle, ArrayList<TargetModel>>> it = this.targetsByIntervalle.iterator(); it.hasNext();)
		{
			Pair<TimeIntervalle, ArrayList<TargetModel>> pair = it.next();
			if(pair.key.include(time))
			{
				return pair.value;
			}
		}
		return result;
	}

	public ArrayList<TargetModel> targetsByIntervalle(TimeIntervalle intervalle) 
	{
		ArrayList<TargetModel> result = new ArrayList<TargetModel>(); 
		for(Iterator<Pair<TimeIntervalle, ArrayList<TargetModel>>> it = this.targetsByIntervalle.iterator(); it.hasNext();)
		{
			Pair<TimeIntervalle, ArrayList<TargetModel>> pair = it.next();
			if(intervalle.contains(pair.key))
			{
				result.addAll(pair.value);
			}
		}
		return result;
	}
	
	public int getTargetSpecificIndex(TimeIntervalle timeIntervalle, TargetModel target)
	{
		int result = 0;
		
		for(Iterator<Pair<TimeIntervalle, ArrayList<TargetModel>>> it = this.targetsByIntervalle.iterator(); it.hasNext();)
		{
			Pair<TimeIntervalle, ArrayList<TargetModel>> pair = it.next();
			if(pair.value.contains(target))
			{
				if(timeIntervalle.contains(pair.key))
				{
					return result;
				}
				result++;
			}
		}
		
		return result;
	}
	

	public int getTargetIndex(TimeIntervalle totalTimeIntervalle, TimeIntervalle intervalle, TargetModel target)
	{
		ArrayList<TargetModel> targets = targetsByIntervalle(totalTimeIntervalle);
		int result = 1;

		for(int i = 0; i < targets.size(); i++)
		{
			if(targets.get(i) == target)
			{
				return result + i;
			}
		}
		return result;
	}
	
	@Override 
	public String toString()
	{
		return "[Door: "+ this.FirstRoom.getID() + " - " + this.SecondRoom.getID() +"]";
	}
}
